/**
 * 智慧小区管理系统 - 支付模块API接口
 * @author Wu.Liang
 * @date 2025-06-26
 * @version 3.0 - 根据系统设计调整方案重构，统一支付接口
 */

import api from '@/api/request'
import type {
  ApiResponse,
  PageResponse,
  PaymentQueryParams,
  PaymentDTO,
  PaymentVO,
  CreatePaymentParams,
  PaymentCallbackParams,
  RefundParams,
  PaymentStatistics,
  PaymentChannel,
  RefundRecord,
  PaymentNotification
} from '@/views/payment/types/payment'

// 支付管理API
export const paymentApi = {
  // 创建支付订单
  createPayment: (data: CreatePaymentParams) => {
    return api.post<ApiResponse<PaymentVO>>('/api/payment/create', data)
  },

  // 分页查询支付记录
  getPaymentList: (params: PaymentQueryParams) => {
    return api.post<ApiResponse<PageResponse<PaymentVO>>>('/api/payment/page', params)
  },

  // 查询支付详情
  getPaymentDetail: (paymentId: number) => {
    return api.get<ApiResponse<PaymentVO>>(`/api/payment/${paymentId}`)
  },

  // 根据订单号查询支付详情
  getPaymentByOrderNo: (orderNo: string) => {
    return api.get<ApiResponse<PaymentVO>>(`/api/payment/order/${orderNo}`)
  },

  // 支付回调处理
  handlePaymentCallback: (data: PaymentCallbackParams) => {
    return api.post<ApiResponse<{ success: boolean }>>('/api/payment/callback', data)
  },

  // 查询支付状态
  getPaymentStatus: (orderNo: string) => {
    return api.get<ApiResponse<{ status: number; statusName: string }>>(`/api/payment/status/${orderNo}`)
  },

  // 取消支付订单
  cancelPayment: (paymentId: number) => {
    return api.post<ApiResponse<{ success: boolean }>>(`/api/payment/${paymentId}/cancel`)
  },

  // 申请退款
  applyRefund: (data: RefundParams) => {
    return api.post<ApiResponse<{ refundId: number; refundNo: string }>>('/api/payment/refund', data)
  },

  // 查询退款记录
  getRefundRecords: (params: { paymentId?: number; current?: number; size?: number }) => {
    return api.get<ApiResponse<PageResponse<RefundRecord>>>('/api/payment/refunds', { params })
  },

  // 获取退款详情
  getRefundDetail: (refundId: number) => {
    return api.get<ApiResponse<RefundRecord>>(`/api/payment/refunds/${refundId}`)
  },

  // 导出支付记录
  exportPayments: (params: PaymentQueryParams) => {
    return api.post('/api/payment/export', params, {
      responseType: 'blob'
    })
  }
}

// 支付统计分析API
export const paymentStatisticsApi = {
  // 获取支付统计信息
  getStatistics: (params?: { communityId?: number; startDate?: string; endDate?: string }) => {
    return api.get<ApiResponse<PaymentStatistics>>('/api/payment/statistics', { params })
  },

  // 获取支付方式统计
  getMethodStatistics: (params?: { communityId?: number; startDate?: string; endDate?: string }) => {
    return api.get<ApiResponse<Array<{
      method: number
      methodName: string
      amount: number
      count: number
      percentage: number
    }>>>('/api/payment/statistics/method', { params })
  },

  // 获取业务类型统计
  getBusinessStatistics: (params?: { communityId?: number; startDate?: string; endDate?: string }) => {
    return api.get<ApiResponse<Array<{
      businessType: number
      businessTypeName: string
      amount: number
      count: number
      percentage: number
    }>>>('/api/payment/statistics/business', { params })
  },

  // 获取每日统计
  getDailyStatistics: (params?: { communityId?: number; startDate?: string; endDate?: string }) => {
    return api.get<ApiResponse<Array<{
      date: string
      amount: number
      count: number
    }>>>('/api/payment/statistics/daily', { params })
  }
}

// 支付渠道管理API
export const paymentChannelApi = {
  // 获取支付渠道列表
  getChannelList: (params?: { isEnabled?: boolean }) => {
    return api.get<ApiResponse<PaymentChannel[]>>('/api/payment/channels', { params })
  },

  // 获取支付渠道详情
  getChannelDetail: (channelId: number) => {
    return api.get<ApiResponse<PaymentChannel>>(`/api/payment/channels/${channelId}`)
  },

  // 创建支付渠道
  createChannel: (data: Partial<PaymentChannel>) => {
    return api.post<ApiResponse<{ channelId: number }>>('/api/payment/channels', data)
  },

  // 更新支付渠道
  updateChannel: (channelId: number, data: Partial<PaymentChannel>) => {
    return api.put<ApiResponse<{ success: boolean }>>(`/api/payment/channels/${channelId}`, data)
  },

  // 删除支付渠道
  deleteChannel: (channelId: number) => {
    return api.delete<ApiResponse<{ success: boolean }>>(`/api/payment/channels/${channelId}`)
  },

  // 启用/禁用支付渠道
  toggleChannelStatus: (channelId: number, isEnabled: boolean) => {
    return api.post<ApiResponse<{ success: boolean }>>(`/api/payment/channels/${channelId}/toggle`, { isEnabled })
  }
}

// 支付通知API
export const paymentNotificationApi = {
  // 获取支付通知列表
  getNotificationList: (params: { current?: number; size?: number; isRead?: boolean }) => {
    return api.get<ApiResponse<PageResponse<PaymentNotification>>>('/api/payment/notifications', { params })
  },

  // 标记通知为已读
  markAsRead: (notificationId: number) => {
    return api.post<ApiResponse<{ success: boolean }>>(`/api/payment/notifications/${notificationId}/read`)
  },

  // 批量标记通知为已读
  batchMarkAsRead: (notificationIds: number[]) => {
    return api.post<ApiResponse<{ successCount: number }>>('/api/payment/notifications/batch-read', { notificationIds })
  },

  // 删除通知
  deleteNotification: (notificationId: number) => {
    return api.delete<ApiResponse<{ success: boolean }>>(`/api/payment/notifications/${notificationId}`)
  },

  // 获取未读通知数量
  getUnreadCount: () => {
    return api.get<ApiResponse<{ count: number }>>('/api/payment/notifications/unread-count')
  }
}

// 统一支付接口
export const unifiedPaymentApi = {
  // 创建统一支付订单
  createUnifiedPayment: (data: {
    userId: number
    communityId: number
    amount: number
    paymentMethod: number
    businessType: number
    businessOrderId: number
    description?: string
    returnUrl?: string
  }) => {
    return api.post<ApiResponse<{
      orderNo: string
      qrCodeUrl?: string
      paymentUrl?: string
      expiresTime: string
    }>>('/api/payment/unified/create', data)
  },

  // 查询统一支付状态
  getUnifiedPaymentStatus: (orderNo: string) => {
    return api.get<ApiResponse<{
      orderNo: string
      status: number
      statusName: string
      amount: number
      paymentMethod: number
      paymentMethodName: string
      paymentTime?: string
      thirdPartyOrderNo?: string
    }>>(`/api/payment/unified/status/${orderNo}`)
  },

  // 统一支付回调
  unifiedPaymentCallback: (data: {
    orderNo: string
    paymentStatus: number
    thirdPartyOrderNo?: string
    paymentTime?: string
    signature?: string
  }) => {
    return api.post<ApiResponse<{ success: boolean }>>('/api/payment/unified/callback', data)
  }
}

// 支付工具API
export const paymentUtilApi = {
  // 验证支付金额
  validateAmount: (amount: number, businessType: number) => {
    return api.post<ApiResponse<{ isValid: boolean; message?: string }>>('/api/payment/validate/amount', {
      amount,
      businessType
    })
  },

  // 获取支付限额
  getPaymentLimits: (paymentMethod: number) => {
    return api.get<ApiResponse<{
      minAmount: number
      maxAmount: number
      dailyLimit: number
      monthlyLimit: number
    }>>(`/api/payment/limits/${paymentMethod}`)
  },

  // 生成支付订单号
  generateOrderNo: (businessType: number) => {
    return api.get<ApiResponse<{ orderNo: string }>>(`/api/payment/generate-order-no/${businessType}`)
  },

  // 检查支付环境
  checkPaymentEnvironment: () => {
    return api.get<ApiResponse<{
      wechatPay: boolean
      alipay: boolean
      bankCard: boolean
      message: string
    }>>('/api/payment/check-environment')
  }
} 